import { createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit"

import { ICardTablesBaseDetailResponse } from "@/api/business/types"

interface GameState {
	tableCode: string
	cardTablesBase: ICardTablesBaseDetailResponse | null
	loading: boolean
}

const initialState: GameState = {
	tableCode: "",
	cardTablesBase: null,
	loading: false
}

const gameSlice = createSlice({
	name: "game",
	initialState,
	reducers: {
		setTableCode: (state, action: PayloadAction<string>) => {
			state.tableCode = action.payload
		},
		setCardTablesBase: (state, action: PayloadAction<ICardTablesBaseDetailResponse>) => {
			state.cardTablesBase = action.payload
		},
		setLoading: (state, action: PayloadAction<boolean>) => {
			state.loading = action.payload
		}
	}
})

export const gameState = (rootState: { game: GameState }): GameState => {
	return rootState.game
}

export const getTableCode = createSelector(gameState, (state: GameState) => state.tableCode)
export const getCardTablesBase = createSelector(gameState, (state: GameState) => state.cardTablesBase)
export const getLoading = createSelector(gameState, (state: GameState) => state.loading)

export const { setTableCode, setCardTablesBase, setLoading } = gameSlice.actions

export default gameSlice.reducer
